<?php
namespace app\avikcm\controller;
use think\Db;
use think\facade\Request;
class System extends Common
{
    /********************************站点管理*******************************/
    //站点设置
    public function system($sys_id=1){
        $table = db('system');
        if(Request::isAjax()) {
            $data = Request::except('file');
            if($table->where('id',1)->update($data)!==false) {
                savecache('System');
                return json(['code' => 1, 'msg' => '站点设置保存成功!', 'url' => url('system/system')]);
            } else {
                return json(array('code' => 0, 'msg' =>'站点设置保存失败！'));
            }
        }else{
            $system = $table->find($sys_id);
            $this->assign('system', json_encode($system,true));
            return $this->fetch();
        }
    }
    public function email(){
        if(Request::isAjax()) {
            $datas = input('post.');
            foreach ($datas as $k=>$v){
               Db::name('config')->where([['name','=',$k],['inc_type','=','smtp']])->update(['value'=>$v]);
            }
            return json(['code' => 1, 'msg' => '邮箱设置成功!', 'url' => url('system/email')]);
        }else{
            $smtp = Db::name('config')->where('inc_type','smtp')->select();
            $info = convert_arr_kv($smtp,'name','value');
            $this->assign('info', json_encode($info,true));
            return $this->fetch();
        }
    }
    public function trySend(){
        $sender = input('email');
        //检查是否邮箱格式
        if (!is_email($sender)) {
            return json(['code' => 0, 'msg' => '测试邮箱码格式有误']);
        }
        $arr = db('config')->where('inc_type','smtp')->select();
        $config = convert_arr_kv($arr,'name','value');
        $content = $config['test_eamil_info'];
        $send = send_email($sender, '测试邮件',$content);
        if ($send) {
            return json(['code' => 1, 'msg' => '邮件发送成功！']);
        } else {
            return json(['code' => 0, 'msg' => '邮件发送失败！']);
        }
    }

    public function express(){
        if(Request::isAjax()){
            $key = input('post.key');
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $paginate = ['list_rows'=>$pageSize,'page'=>$page];
            $list = db('express_company')->where('name', 'like', "%" . $key . "%")->paginate($paginate)->toArray();
            return ['code'=>0,'msg'=>'成功','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        return $this->fetch('express_list');
    }

    // 添加新闻
    public function addExpress(){
        if(Request::isAjax()){
            //构建数组
            $data = Request::except('file');
            db('express_company')->insert($data);
            $result['code'] = 1;
            $result['msg'] = '添加成功!';
            $result['url'] = url('express');
            return $result;
        }else{
            $this->assign('title','添加快递公司');
            $this->assign('info','null');
            return $this->fetch('express_form');
        }
    }

    // 修改新闻
    public function editExpress(){
        if(Request::isAjax()){
            //构建数组
            $data = Request::except('file');
            db('express_company')->update($data);
            $result['code'] = 1;
            $result['msg'] = '修改成功!';
            $result['url'] = url('express');
            return $result;
        }else{
            $id = input('id');
            $info = db('express_company')->where('id',$id)->find();
            $this->assign('info',json_encode($info,true));
            $this->assign('baseInfo',$info);
            $this->assign('title','修改快递公司');
            return $this->fetch('express_form');
        }
    }

    // 删除
    public function delExpress(){
        $id = input('id');
        db('express_company')->where('id',$id)->delete();
        return ['code'=>1,'msg'=>'删除成功！'];
    }

    //修改联系方式列表
    public function contactList()
    {
        if (Request::isAjax()) {
            $list = [
                ['id'=> 1, 'title'=>'联系方式一', 'inc_type'=>'contact1'],
                ['id'=> 2, 'title'=>'联系方式二', 'inc_type' => 'contact2'],
                ['id'=> 3, 'title'=>'联系方式三', 'inc_type' => 'contact3']
            ];
            return ['code' => 0, 'msg' => '成功', 'data' => $list, 'rel' => 1];
        } 
        return $this->fetch();
    }

    //修改联系方式
    public function contact()
    {
        $inc_type = input('inc_type');
        if (Request::isAjax()) {
            $datas = input('post.');
            foreach ($datas as $k => $v) {
                if($k == 'file' || $k == 'inc_type') continue;
                $row = db('config')->where([['name', '=', $k], ['inc_type', '=', $inc_type]])->find();
                if($row){
                    Db::name('config')->where([['name', '=', $k], ['inc_type', '=', $inc_type]])->update(['value' => $v]);
                }
                else{
                    Db::name('config')->insert(
                        ['name'=>$k, 'inc_type'=>$inc_type, 'value' => $v]
                    );
                }
            }
            return json(['code' => 1, 'msg' => '联系方式设置成功!', 'url' => url('system/contactList')]);
        } else {
            $smtp = Db::name('config')->where('inc_type', $inc_type)->select();
            $info = convert_arr_kv($smtp, 'name', 'value');
            $this->assign('baseInfo', $info);
            $this->assign('inc_type', $inc_type);
            $this->assign('info', json_encode($info, true));
            return $this->fetch();
        }
    }

}
